﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.ServiceProcess;
using System.Threading;
using PSE.Deployment.SetupBase.Configuration;
using PSE.Deployment.SetupBase.Parameter;
using PSE.Deployment.Engine.CustomExceptions;
using PSE.Deployment.Engine.Interfaces;

namespace PSE.Deployment.SetupBase.Steps
{
    public class StartInstance : SetupStep<Configuration.Actions.InstallProcessManagerInstance>
    {
        public bool Start { get; set; }

        public StartInstance(IStepGroup parent, Group group, Configuration.Actions.InstallProcessManagerInstance action, Parameter.Enums.SetupMode setupMode, bool start)
            : base(parent, group, action, setupMode)
        {
            this.Id = this.Action.Id + "-StartInstance-" + start.ToString().ToUpper();
            this.Start = start;
            this.ResetProgress(Action.Instances.Count);
            this.Description = "Verficação das instâncias";
        }

        public override void Execute()
        {
            this.ResetProgress(Action.Instances.Count);

            if (this.SetupMode == Parameter.Enums.SetupMode.Install ||
                this.SetupMode == Parameter.Enums.SetupMode.Uninstall)
            {
                StartStopInstances();
            }
        }

        public override void Undo()
        {

        }

        private void StartStopInstances()
        {
            foreach (ProcessManagerInstance instance in Action.Instances)
            {
                if (this.IsStopped())
                    return;

                this.IncrementProgress(instance.InstanceName);

                try
                {
                    if (Start)
                    {
                        if (PSE.Deployment.Util.ProcessManager.GetInstance().GetStatus() == System.ServiceProcess.ServiceControllerStatus.Stopped)
                        {
                            PSE.Deployment.Util.ProcessManager.GetInstance().Start();
                        }
                        else
                        {
                            PSE.Deployment.Util.ProcessManager.GetInstance().StartInstance(instance.ProductName, instance.InstanceName);
                        }
                    }
                    else
                    {
                        if (PSE.Deployment.Util.ProcessManager.GetInstance().GetStatus() == System.ServiceProcess.ServiceControllerStatus.Running)
                        {
                            PSE.Deployment.Util.ProcessManager.GetInstance().StopInstance(instance.ProductName, instance.InstanceName);
                        }
                    }
                }
                catch (Exception ex)
                {
                    SetupMainFactory.GetInstance().WriteStepLog(this, string.Format("Não foi possível verificar a instância {0} do produto {1}", instance.InstanceName, instance.ProductName), ex);
                }
            }
        }
    }
}
